import requestMeeting, { request } from '@/plugins/axios'
import { API_KIS_ROOM_PREFIX } from '@/utils/constant'
import {
	CheckLockParam,
	FetchAllMeetingDetail,
	FetchLoadMeetingRoom,
	FetchMeetingRoomParams,
	FetchRepeatInfo,
	FetchDevices,
	FetchLockMeetingRoom,
	FetchMeetingRoom,
	UserEntity,
	SaveMeetingParams,
	SaveTemplateParams,
} from './interface'
import dayjs from '@/plugins/dayjs'

export const fetchIsNotExist = async (meeting_moid: string) =>
	requestMeeting({
		url: `/meetings/is_not_exit`,
		method: 'get',
		params: { meeting_moid },
	}).then((res) => res)

/** 获取会议规模 */
export const fetchResources = async () =>
	request<{
		resource_type: number
		resources: {
			name: string
			max_join_mt: number
			total: number
			used: number
		}
	}>({
		url: `/resources`,
		method: 'get',
	}).then((res) => res)

/** type 1-会议 2-模板 */
export const fetchPreKey = async (meeting_moid: string, type: 1 | 2) =>
	requestMeeting<string>({
		url: `/meetings/pre_key`,
		method: 'get',
		params: { condition: meeting_moid, type },
	}).then((res) => (res.success && res.data ? res.data : ''))

/** 获取会议信息（全量） */
export const fetchGetAllMeetingDetail = async (meetingMoid: string) =>
	request<FetchAllMeetingDetail>({
		url: '/meetings/' + meetingMoid + '/all',
		method: 'get',
	}).then((res) => (res.success ? res : undefined))

/** 检验模板名称 success 1-不存在 0-存在*/
export const fetchCheckTemplateName = async (templateName: string, templateMoid: string) =>
	requestMeeting({
		url: `/personal_templates/valid_conf_name`,
		method: 'get',
		params: { template_name: templateName, template_moid: templateMoid },
	}).then((res) => res)

/** 获取区域列表 */
export const fetchLoadRegions = async () =>
	request<{ meeting_room_areas: { area: string; id: string }[] }>({
		url: `meeting_room_areas`,
		method: 'GET',
		params: { start: 0, count: 0 },
		baseURL: API_KIS_ROOM_PREFIX,
	}).then((res) => (res.success && res.meeting_room_areas ? res.meeting_room_areas : []))

/** 获取区域列表会议室 */
export const fetchLoadMeetingRoomByRegion = async (regionId: number) =>
	requestMeeting<FetchMeetingRoom[]>({
		url: `meeting_rooms`,
		method: 'post',
		data: { params: JSON.stringify({ start: 0, region_id: regionId, status: 1, count: 0 }) },
		baseURL: API_KIS_ROOM_PREFIX,
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded',
		},
	}).then((res) => (res.success && res.data ? res.data : []))

/** 查询会议室 */
export const fetchGetMeetingRoom = async (regionId: string) =>
	requestMeeting<{
		id: string
		name: string
		people_admit: number
		area: number
		phone_no: string
		video: 0 | 1
		e164: string
		device_admin: string
		admin_phone_no: string
		remark: string
		region: string
		region_name: string
		status: number
		device_ids: string
	}>({
		url: `meeting_room/${regionId}`,
		method: 'GET',
		baseURL: API_KIS_ROOM_PREFIX,
	}).then((res) => (res.success && res.data ? res.data : undefined))

/** 获取设备选项 */
export const fetchDevices = async () =>
	requestMeeting<FetchDevices[]>({
		url: `devices`,
		method: 'GET',
		baseURL: API_KIS_ROOM_PREFIX,
	}).then((res) => (res.success && res.data ? res.data : []))

/** 修改时间或会议室类型时校验会议室 */
export const fetchCheckRoomBookOrLockedCheck = async (data: CheckLockParam) =>
	requestMeeting<boolean>({
		url: `/room_booked_or_locked_check`,
		method: 'post',
		data: { params: JSON.stringify(data) },
		baseURL: API_KIS_ROOM_PREFIX,
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded',
		},
	}).then((res) => res)

/** 查询是否存在锁定的会议室 */
export const fetchMeetingRoomLockExist = async (data: { meeting_form_key: string }) =>
	request<{ exist: boolean }>({
		url: `/meeting_room_lock_exist`,
		method: 'post',
		data: { params: JSON.stringify(data) },
		baseURL: API_KIS_ROOM_PREFIX,
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded',
		},
	}).then((res) => res)

/** 查询是否有锁定和预定的会议室 */
export const fetchCheckRoomBookOrLocked = async (data: { id: string }) =>
	requestMeeting<boolean>({
		url: `/room_booked_or_locked`,
		method: 'post',
		data: { params: JSON.stringify(data) },
		baseURL: API_KIS_ROOM_PREFIX,
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded',
		},
	}).then((res) => res)

/** 解锁会议室 */
export const fetchUnLockMeetingRoom = async (data: { meeting_form_key: string }) =>
	requestMeeting<boolean>({
		url: `/meeting_room_unlock`,
		method: 'post',
		data: { params: JSON.stringify(data) },
		baseURL: API_KIS_ROOM_PREFIX,
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded',
		},
	}).then((res) => res)

/** 批量锁定会议室 */
export const fetchLockMeetingRoom = async (list: FetchLockMeetingRoom[]) =>
	request<{ meeting_form_key: string }>({
		url: `/meeting_room_batch_lock`,
		method: 'post',
		data: { params: JSON.stringify({ list }) },
		baseURL: API_KIS_ROOM_PREFIX,
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded',
		},
	}).then((res) => (res.success && res.meeting_form_key ? res.meeting_form_key : ''))

/** 删除已经预定的会议室 */
export const fetchDeleteBookedMeetingRoom = async (data: { meeting_moid: string }) =>
	requestMeeting<boolean>({
		url: `/meeting_room_book_cancel`,
		method: 'post',
		data: { params: JSON.stringify(data) },
		baseURL: API_KIS_ROOM_PREFIX,
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded',
		},
	}).then((res) => res)

/** 删除已经预定的会议室(通知后台) */
export const fetchDeleteMeetingRoom = async (meeting_moid: string) =>
	requestMeeting({
		url: `/meetings/${meeting_moid}/meeting_rooms`,
		method: 'DELETE',
	}).then((res) => res)

/** 获取非重复的视频会议的会议室 */
export const fetchLoadMeetingRoom = async (data: FetchMeetingRoomParams[]) =>
	requestMeeting<FetchLoadMeetingRoom>({
		url: `/meeting_room_by_video`,
		method: 'post',
		data: { params: JSON.stringify(data) },
		baseURL: API_KIS_ROOM_PREFIX,
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded',
		},
	}).then((res) => res)

/** 检测例会是否能生成 */
export const fetchCheckRegularMeeting = async (data: {
	frequency: number
	week_days: string
	start_date: string
	end_date: string
}) => {
	let start_date = dayjs(data.start_date).tz().format('YYYY-MM-DD')
	let end_date = dayjs(data.end_date).tz().format('YYYY-MM-DD')
	return requestMeeting<boolean>({
		url: `/regular_meeting/check_repeat_meeting`,
		method: 'post',
		data: { ...data, start_date, end_date },
	}).then((res) => res)
}

/** 获取重复的视频会议的会议室(例会) */
export const fetchLoadRegularMeetingRoom = async (data: {
	search_meeting_room_by_video: FetchMeetingRoomParams[]
	repeat: FetchRepeatInfo
}) =>
	requestMeeting<FetchLoadMeetingRoom>({
		url: `regular_meeting/meeting_room_by_video`,
		method: 'post',
		data: { params: JSON.stringify(data) },
		baseURL: API_KIS_ROOM_PREFIX,
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded',
		},
	}).then((res) => res)

/** 查询当前页面是否有锁定和预定的会议室 */
export const fetchCheckExistRoomBookedOrLockedForCurrentPage = async (data: {
	meeting_moid: string
	meeting_form_key: string
}) =>
	requestMeeting<boolean>({
		url: `room_booked_or_locked_for_current_page`,
		method: 'post',
		data: { params: JSON.stringify(data) },
		baseURL: API_KIS_ROOM_PREFIX,
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded',
		},
	}).then((res) => res)

/** 保存会议 */
export const fetchSaveMeeting = async (data: SaveMeetingParams) => {
	return request<{
		conf_id: string
		meeting_id: string
		machine_room_moid: string
	}>({
		url: `/booked_confs`,
		method: 'post',
		data: { ...data, start_time: dayjs(data.start_time).tz().format() },
	}).then((res) => res)
}

/** 编辑会议 */
export const fetchEditMeeting = async (meetingMoid: string, data: SaveMeetingParams) =>
	request<{
		conf_id: string
		meeting_id: string
		machine_room_moid: string
	}>({
		url: `/booked_confs/${meetingMoid}`,
		method: 'post',
		data: data,
	}).then((res) => res)

/** 保存例会 */
export const fetchSaveRegularMeeting = async (data: SaveMeetingParams) =>
	request<{
		conf_id: string
		meeting_id: string
		machine_room_moid: string
	}>({
		url: `/regular_meeting`,
		method: 'post',
		data: data,
	}).then((res) => res)

/** 修改例会 */
export const fetchEditRegularMeeting = async (meetingMoid: string, data: SaveMeetingParams) =>
	request<{
		conf_id: string
		meeting_id: string
		machine_room_moid: string
	}>({
		url: `/regular_meeting/${meetingMoid}`,
		method: 'post',
		data: data,
	}).then((res) => res)

/** 保存会议模板 */
export const fetchSaveTpl = async (preKey: string, data: SaveTemplateParams) =>
	request<{ template_id: string }>({
		url: `/personal_templates?pre_key=${preKey}`,
		method: 'post',
		data: data,
	}).then((res) => res)

/** 编辑会议模板 */
export const fetchEditTpl = async (template_id: string, preKey: string, data: SaveTemplateParams) =>
	request<{ template_id: string }>({
		url: `/personal_templates/${template_id}?pre_key=${preKey}`,
		method: 'post',
		data: data,
	}).then((res) => res)

export const fetchCascadeData = async (is_templete: boolean, cascades: string[], template_moid?: string) =>
	requestMeeting<
		{
			e164: string
			is_simple: 0 | 1
			mts: UserEntity[]
		}[]
	>({
		url: is_templete ? `/personal_templates/template_mt?template_moid=${template_moid}` : `/meetings/mt_by_e164`,
		method: 'post',
		data: cascades,
	}).then((res) => (res.success && res.data ? res.data : []))

/** 获取录播服务器 */
export const fetchGetVrs = async () =>
	requestMeeting({
		url: `/mcc/recorders/servers`,
		method: 'get',
	}).then((res) => res)
